from flask import Flask, request, Response, stream_with_context
from flask_cors import CORS, cross_origin
from concurrent.futures import ThreadPoolExecutor
import os
import sys
import time

filepath = os.path.abspath(__file__)
dirpath = os.path.dirname(filepath)
print(filepath, dirpath)

executor = ThreadPoolExecutor(10)

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
CORS(app)

app.config['STATIC_FOLDER'] = 'static'
app.config['STATIC_URL_PATH'] = '/static'


@app.route('/api/')
def hello_world():
    return 'Hello, World!'


@app.route('/api/qwen', methods=['GET', 'POST'])
def qwen_stream():
    def generate():
        while True:
            yield 'data: i = %d\n' % time.time()
            time.sleep(1)

    response = Response(generate(), mimetype='text/event-stream; charset=tf-8')
    # response.headers['Cache-Control'] = 'no-cache'
    # response.headers['Connection'] = 'keep-alive'
    # response.headers['Access-Control-Allow-Origin'] = '*'
    # response.headers['Access-Control-Allow-Credentials'] = "true"
    # response.headers['Tranfer-Encoding'] = 'chunked'
    # return stream_with_context(generate())
    return response


if __name__ == '__main__':
    isFlask = True
    if isFlask:
        app.run(host="0.0.0.0")
    else:
        bertinfer.main()
